<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link href="../../../jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>

    <script type="text/javascript" src="../../../jquery/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../../../jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
</head>
<body>

<div>
    <div style="position: relative; left: 30px; top: -10px;">
        <div class="page-header">
            <h3>字典值列表</h3>
        </div>
    </div>
</div>
<div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;left: 30px;">
    <div class="btn-group" style="position: relative; top: 18%;">
        <button type="button" class="btn btn-primary" onclick="window.location.href='save.html'"><span
                class="glyphicon glyphicon-plus"></span> 创建
        </button>
        <button  id="btnEdit"   type="button" class="btn btn-default" ><span
                class="glyphicon glyphicon-edit"></span> 编辑
        </button>
        <button id="btnDel" type="button" class="btn btn-danger"><span class="glyphicon glyphicon-minus"></span> 删除</button>
    </div>
</div>
<div style="position: relative; left: 30px; top: 20px;">
    <table id="table" class="table table-hover">
        <thead>
        <tr style="color: #B3B3B3;">
            <td><input id="selectAll" type="checkbox"/></td>
            <td>序号</td>
            <td>字典值</td>
            <td>文本</td>
            <td>排序号</td>
            <td>字典类型名称</td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><input type="checkbox"/></td>
            <td>1</td>
            <td>m</td>
            <td>男</td>
            <td>1</td>
            <td>sex</td>
        </tr>
        </tbody>
    </table>
</div>

</body>
</html>

<script>

    function getAll() {
        $.ajax({
            url: "/DictionaryValue/getAll.action",
            type: "get",
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            },
            success: function (data, textStatus, jqXHR) {
                $("#table tbody").empty();
                $(data).each(function (i, e) {
                    var tamp = '        <tr>\n' +
                        '            <td><input name="code"  value="'+e.id+'"  type="checkbox"/></td>\n' +
                        '            <td>' + (i + 1) + '</td>\n' +
                        '            <td>' + e.value + '</td>\n' +
                        '            <td>' + e.text + '</td>\n' +
                        '            <td>' + e.orderNo + '</td>\n' +
                        '            <td>' + e.dictionaryType.name + '</td>\n' +
                        '        </tr>';
                    $("#table tbody").append(tamp);
                });
            },
            complete: function (XMLHttpRequest, textStatus) {
            }
        });


    }


    $(function () {

        getAll();

        //checkBox全选
        $("#selectAll").click(function (){
            $(":checkbox[name=code]").prop("checked",$("#selectAll").prop("checked"));
        });


        $("#btnDel").click(function () {

            //jQuery数组对象
            var $delCode=$(":checkbox[name=code]:checked");

            if($delCode.size()==0){
                window.alert("你没有选任何类型，无法删除");
                return;
            }

            if(!window.confirm("确定删除吗？")){
                return;
            }
            //确定要删除了
            var codes=[];
            //循环所有的CheckBox对象
            $delCode.each(function (i,e) {
                //获取每个CheckBox对象的value值，并且添加到codes数组
                codes.push(e.value);
            });
            //把数组变成字符串自己传递给后端，避免前端告诉知道传递的是JSON格式
            //var ids = codes.join(",");
            //前端传递的JSON数据不管是原生态还是jQuery对象，都需要使用JSON.stringify格式化
            var codes = JSON.stringify(codes);
            $.ajax({
                url:"/DictionaryValue/del.action",
                type:"post",
                dataType:"json",
                contentType:"application/json",
                data:codes,
                beforeSend:function (XMLHttpRequest) {

                },
                error:function (XMLHttpRequest,textStatus, errorThrown) {

                },
                success:function (data, textStatus, jqXHR) {
                    if(data['result']=="success"){
                        getAll();
                    }
                    else{
                        window.alert(data['mess']);
                    }
                },
                complete:function (XMLHttpRequest, textStatus) {

                }
            });
        });


        $("#btnEdit").click(function () {
            //jQuery数组对象
            var $delCode=$(":checkbox[name=code]:checked");

            if($delCode.size()==0){
                window.alert("你没有选任何类型，无法编辑");
                return;
            }
            window.location="edit.html?id="+$($delCode[0]).val();
        });


    });


</script>